package cn.jbit.supermarker.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import cn.jbit.supermarker.util.ConfigManager;


public class BaseDao {
	protected Connection connection = null;
	protected PreparedStatement ps = null;
	protected ResultSet rs = null;

	// 获取数据库的连接
	public boolean getConnection() {
		String driver = ConfigManager.getInstance().getString(
				"jdbc.driver_class");
		String url = ConfigManager.getInstance().getString(
				"jdbc.connection.url");
		String username = ConfigManager.getInstance().getString(
				"jdbc.connection.username");
		String userpwd = ConfigManager.getInstance().getString(
				"jdbc.connection.password");
		try {
			Class.forName(driver);
			connection = DriverManager.getConnection(url, username, userpwd);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	/*public int ExecuteUpdate(String sql, Object[] params) {
		int updateRows = 0;
		if (this.getConnection()) {
			try {
				connection.prepareStatement(sql);
				for(int i=0;i<params.length;i++){
					ps.setObject(i+1, params[i]);
				}
				updateRows=ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				this.closeResource();
			}
		}
		return updateRows;
	}*/
	public int ExecuteUpdate(String sql,Object[] params){
		 int updateRows=0;
		 if(this.getConnection()){
			 try {
				ps=connection.prepareStatement(sql);
				for(int i=0;i<params.length;i++){
					ps.setObject(i+1, params[i]);
				}
				updateRows=ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				
				this.closeResource();
			}
			 	 
		 }
		 
		return updateRows;	 
		 
	 }
	public boolean getConnection1() {

		try {
			Context cxt = new InitialContext();
			DataSource ds = (DataSource) cxt.lookup("java:comp/env/jdbc/smbms");
			connection = ds.getConnection();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	public ResultSet executeSQL(String sql, Object[] params) {
		if (this.getConnection()) {
			try {
				ps = connection.prepareStatement(sql);
				// 填充占位符
				for (int i = 0; i < params.length; i++) {
					ps.setObject(i + 1, params[i]);
				}
				rs = ps.executeQuery();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return rs;
	}
	
	public boolean closeResource() {
		try {
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}
			if (connection != null) {
				connection.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

}
